import calendar
import datetime
import random

from flask import Blueprint, current_app
from flask_cors import cross_origin
from flask import request

from app.Services.AdminServices.UserServices.UserServices import UserServices
from app.Services.HealthServices.WeightServices.WeightServices import WeightServices
from app.Utils.Utils import success_response, error_response, return_response_

# 1. 创建蓝图对象
weight_bp = Blueprint(
    name='weight_bp',
    import_name=__name__,
    url_prefix='/health/weight'
)
weight_services = WeightServices()


# 更新体重
@weight_bp.route('/submit_weight', methods=['POST'])
def submit_weight():
    """
    根据用户ID和日期更新体重kg值，如果日期不存在，则新增。
    """
    user_id = int(request.cookies.get('user_id'))
    weight = float(request.json.get('weight'))
    date = datetime.datetime.now()
    status, res = weight_services.add_or_update_weight(user_id, weight, date)
    return return_response_(status, res)


# 计算某个用户某日的基础代谢值
@weight_bp.route('/get_bmr', methods=['POST'])
def get_bmr():
    """
    根据用户ID、用户某日体重，计算该用户当天的基础代谢值BMR。
    """
    user_id = int(request.cookies.get('user_id'))
    weight_date = request.json.get('date')
    status, res = weight_services.cal_bmr(user_id, bmr_date=weight_date)
    print(res)
    return return_response_(status, res)
